﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VietPremier.Areas.AdminCP.Models;
using MvcLibrary.Repository;
using System.IO;

namespace VietPremier.Areas.AdminCP.Controllers
{
    public class AdminPromotionController : Controller
    {
        #region "Index"
        public ActionResult Index()
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                PromotionModel model = new PromotionModel();

                foreach (var promotion in db.EntityPromotions)
                {
                    model.listPromotion.Add(new Promotion()
                    {
                        Id = promotion.Id,
                        Name = promotion.Name,
                        Description = promotion.Description,
                        Itinerary = promotion.Itinerary,
                        Note = promotion.Note,
                        Show = promotion.Show,
                        Image = promotion.Image,
                        //<!-- SEO -->
                        Titletag = promotion.Titletag,
                        Meta_description = promotion.Meta_description,
                        Meta_keyword = promotion.Meta_keyword
                    });
                }
                return View(model);
            }
        }
        #endregion //index

        #region "Edit"
        public ActionResult Edit(int idPromotion)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                Promotion model = new Promotion();

                EntityPromotion promotion = db.EntityPromotions.FirstOrDefault(o => o.Id == idPromotion);
                if (promotion != null)
                {
                    model.Id = promotion.Id;
                    model.Name = promotion.Name;
                    model.Description = promotion.Description;
                    model.Itinerary = promotion.Itinerary;
                    model.Note = promotion.Note;
                    model.Image = promotion.Image;
                    //<!--SEO -->
                    model.Titletag = promotion.Titletag;
                    model.Meta_description = promotion.Meta_description;
                    model.Meta_keyword = promotion.Meta_keyword;
                }
                return View(model);
            }
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(int idPromotion, Promotion model, HttpPostedFileBase Image )
        {

            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                EntityPromotion promotion = db.EntityPromotions.Where(p => p.Id == idPromotion).FirstOrDefault();
                if (promotion != null)
                {
                    promotion.Name = model.Name;
                    promotion.Description = model.Description;
                    promotion.Itinerary = model.Itinerary;
                    promotion.Note = model.Note;
                    if (Image != null && Image.ContentLength > 0)
                    {
                        int kb = Image.ContentLength / 1024; //file size kb
                        if (kb >= 2048) // file qua lon
                        {
                            return RedirectToAction("Index", "Error", new { errorMsg = "Images must be less than 2MB." });
                        }
                        //delete file cu
                        if (promotion.Image != null)
                        {
                            string fileToDelete = Path.Combine(Server.MapPath("~/Content/upload/promotions/"), promotion.Image); // file hinh cu
                            System.IO.File.Delete(fileToDelete);
                        }

                        ImageHelper imgHelper = new ImageHelper();
                        string encodestring = imgHelper.encodeImageFile(Image);

                        if (encodestring == "!")
                            return RedirectToAction("Index", "Error", new { errorMsg = "Can not upload image" });

                        var path = Path.Combine(Server.MapPath("~/Content/upload/promotions/"), encodestring);
                        Image.SaveAs(path);

                        promotion.Image = encodestring;
                    }
                    //<!--SEO -->
                    promotion.Titletag = model.Titletag;
                    promotion.Meta_description = model.Meta_description;
                    promotion.Meta_keyword = model.Meta_keyword;

                    db.SubmitChanges();
                    return RedirectToAction("Index", "AdminPromotion");
                }
                return RedirectToAction("Edit", "AdminPromotion");
            }
        }

        #endregion //edit

        #region "Delete"
        public string Delete(int id)
        {
            try
            {
                dbVietPremierDataContext db = new dbVietPremierDataContext();
                EntityPromotion item = db.EntityPromotions.Where(p => p.Id == id).Single();
                if (item.Image != null)
                {
                    string fileToDelete = Path.Combine(Server.MapPath("~/Content/upload/promotions/"), item.Image); // file hinh cu
                    System.IO.File.Delete(fileToDelete);
                }

                db.EntityPromotions.DeleteOnSubmit(item);
                db.SubmitChanges();
                return "Ok";
            }
            catch
            {
                return "Error";

            }

        }
        #endregion

        #region "Insert"
        public ActionResult Insert()
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                Promotion model = new Promotion();

                return PartialView(model);
            }
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Insert(Promotion model, HttpPostedFileBase Image)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                EntityPromotion promotion = new EntityPromotion();

                promotion.Name = model.Name;
                promotion.Description = model.Description;
                promotion.Itinerary = model.Itinerary;
                promotion.Note = model.Note;
                promotion.Show = "Hidden";
                if (Image != null && Image.ContentLength > 0)
                {
                    int kb = Image.ContentLength / 1024; //file size kb
                    if (kb >= 2048) // file qua lon
                    {
                        return RedirectToAction("Index", "Error", new { errorMsg = "Can not upload image" });
                    }

                    ImageHelper imgHelper = new ImageHelper();
                    string encodestring = imgHelper.encodeImageFile(Image);

                    if (encodestring == "!")
                        return RedirectToAction("Index", "Error", new { errorMsg = "Can not upload image" });

                    var path = Path.Combine(Server.MapPath("~/Content/upload/promotions/"), encodestring);
                    Image.SaveAs(path);

                    promotion.Image = encodestring;
                }

                //<!-- SEO -->
                promotion.Titletag = model.Titletag;
                promotion.Meta_description = model.Meta_description;
                promotion.Meta_keyword = model.Meta_keyword;

                db.EntityPromotions.InsertOnSubmit(promotion);
                db.SubmitChanges();

                return RedirectToAction("Index", "AdminPromotion");
            }
        }
        #endregion

        [HttpPost]
        public ActionResult SetStatus(int id)
        {
            dbVietPremierDataContext db = new dbVietPremierDataContext();

            EntityPromotion promotion = db.EntityPromotions.FirstOrDefault(o => o.Id == id);
            if (promotion.Show == "Hidden")
            {
                promotion.Show = "Show";
            }
            else
            {
                promotion.Show = "Hidden";
            }
            db.SubmitChanges();

            var count = db.EntityPromotions.Where(o => o.Show == "Show").Count();
            if (count <= 1)
            {
                
                return Content("Ok");
                
            }
            else
            {
                promotion = db.EntityPromotions.FirstOrDefault(o => o.Id == id);
                promotion.Show = "Hidden";
                db.SubmitChanges();
                return Content("Error");
            }
            
        }
    }
}
